package com.dicsystems.myrec;

import java.util.List;

import com.dicsystems.myrec.dao.CategoryDao;
import com.dicsystems.myrec.dao.TodoDao;
import com.dicsystems.myrec.dto.Todo;

import android.R.drawable;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.ImageButton;

public class TodoListActivity extends ListActivity {

	private Context me = this;
	private Common co = new Common(me);
	private TodoDao td = new TodoDao(me);
	private CategoryDao cd = new CategoryDao(me);

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.todo_list);
	}

	@Override
	protected void onResume() {
		super.onResume();
		td.open();
		cd.open();
		setList(td.select());
	}

	@Override
	protected void onPause() {
		super.onPause();
		td.close();
		cd.close();
	}

	private void setList(final List<Todo> todos) {
		getListView().setAdapter(new TodoAdapter(me, R.layout.todo_list_row, todos));
		getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				startActivity(new Intent(me, DetailsActivity.class).putExtra("todo", todos.get(position)));
			}
		});
	}

	public void search(View view) {
		new SearchDialog();
	}

	public void close(View view) {
		System.exit(RESULT_OK);
	}

	private class SearchDialog extends Dialog {

		private Button start_date;
		private ImageButton clear_start_date;
		private Button end_date;
		private ImageButton clear_end_date;
		private Button category;
		private ImageButton clear_category;
		private CheckBox last;
		private ImageButton search;
		private ImageButton close;

		private SearchDialog() {
			super(me);
			getWindow().requestFeature(Window.FEATURE_NO_TITLE);
			setContentView(R.layout.search);
			start_date = (Button)findViewById(R.id.start_date);
			clear_start_date = (ImageButton)findViewById(R.id.clear_start_date);
			end_date = (Button)findViewById(R.id.end_date);
			clear_end_date = (ImageButton)findViewById(R.id.clear_end_date);
			category = (Button)findViewById(R.id.category);
			clear_category = (ImageButton)findViewById(R.id.clear_category);
			last = (CheckBox)findViewById(R.id.last);
			search = (ImageButton)findViewById(R.id.search);
			close = (ImageButton)findViewById(R.id.close);
			start_date.setOnFocusChangeListener(new Button.OnFocusChangeListener() {
				@Override
				public void onFocusChange(View v, boolean hasFocus) {
					if(hasFocus) {
						showPicker(start_date, clear_start_date, R.string.start_date);
					}
				}
			});
			start_date.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View v) {
					showPicker(start_date, clear_start_date, R.string.start_date);
				}
			});
			clear_start_date.setOnClickListener(new ImageButton.OnClickListener() {
				@Override
				public void onClick(View v) {
					start_date.setText(null);
					clear_start_date.setVisibility(View.GONE);
				}
			});
			end_date.setOnFocusChangeListener(new Button.OnFocusChangeListener() {
				@Override
				public void onFocusChange(View v, boolean hasFocus) {
					if(hasFocus) {
						showPicker(end_date, clear_end_date, R.string.end_date);
					}
				}
			});
			end_date.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View v) {
					showPicker(end_date, clear_end_date, R.string.end_date);
				}
			});
			clear_end_date.setOnClickListener(new ImageButton.OnClickListener() {
				@Override
				public void onClick(View v) {
					end_date.setText(null);
					clear_end_date.setVisibility(View.GONE);
				}
			});
			category.setOnFocusChangeListener(new Button.OnFocusChangeListener() {
				@Override
				public void onFocusChange(View v, boolean hasFocus) {
					if(hasFocus) {
						new CategoryDialog(me, cd.select(), category, clear_category).show();
					}
				}
			});
			category.setOnClickListener(new Button.OnClickListener() {
				@Override
				public void onClick(View v) {
					new CategoryDialog(me, cd.select(), category, clear_category).show();
				}
			});
			clear_category.setOnClickListener(new ImageButton.OnClickListener() {
				@Override
				public void onClick(View v) {
					category.setTag(null);
					category.setText(null);
					clear_category.setVisibility(View.GONE);
				}
			});
			search.setOnClickListener(new ImageButton.OnClickListener() {
				@Override
				public void onClick(View v) {
					if(!co.text(start_date).equals("") && !co.text(end_date).equals("")) {
						if(co.calendar(start_date).compareTo(co.calendar(end_date)) > 0) {
							co.error(R.string.error_date);
							return;
						}
					}
					setList(td.select(sql(), null));
					dismiss();
				}
			});
			close.setOnClickListener(new ImageButton.OnClickListener() {
				@Override
				public void onClick(View v) {
					dismiss();
				}
			});
			show();
		}

		private void showPicker(final Button date, final ImageButton delete_date, int title) {
			View view = LayoutInflater.from(me).inflate(R.layout.date, null);
			final DatePicker date2 = (DatePicker)view.findViewById(R.id.date);
			if(co.text(date).equals("")) {
				date2.init(co.year(), co.month() - 1, co.day(), null);
			}
			else {
				date2.init(co.year(date), co.month(date) - 1, co.day(date), null);
			}
			AlertDialog.Builder builder = new AlertDialog.Builder(me);
			builder.setIcon(drawable.ic_dialog_dialer);
			builder.setTitle(title);
			builder.setView(view);
			builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					date.setText(co.getDate(date2));
					delete_date.setVisibility(View.VISIBLE);
				}
			});
			builder.setNegativeButton(R.string.cancel, null);
			builder.show();
		}

		private String sql() {
			String sql = "SELECT todo.*, category.name FROM todo INNER JOIN category ON todo.category_id = category._id WHERE";
			if(co.text(start_date).equals("")) {
				if(co.text(end_date).equals("")) {
					sql += " todo.start_date BETWEEN '1900-01-01' AND '2100-12-31'";
				}
				else {
					sql += " todo.start_date BETWEEN '1900-01-01' AND '" + co.text(end_date) + "'";
				}
			}
			else {
				if(co.text(end_date).equals("")) {
					sql += " todo.start_date BETWEEN '" + co.text(start_date) + "' AND '2100-12-31'";
				}
				else {
					sql += " todo.start_date BETWEEN '" + co.text(start_date) + "' AND '" + co.text(end_date) + "'";
				}
			}
			if(category.getTag() != null) {
				sql += " AND todo.category_id = '" + co.tag(category) + "'";
			}
			sql += "  ORDER BY todo.start_date, todo.start_time, todo._id";
			if(last.isChecked()) {
				sql += " LIMIT 10";
			}
			return sql;
		}
	}
}
